package com.ruoyi.community.utils;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.Announce;
import com.ruoyi.community.domain.MessageAnnounce;
import com.ruoyi.community.domain.MessageRemind;
import com.ruoyi.community.domain.Remind;
import com.ruoyi.community.service.message.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class MessageUtil {

    @Autowired
    MessageService messageService;

    public static MessageUtil messageUtil;

    @PostConstruct
    public void init() {
        messageUtil = this;
    }

    /**
     * 新增公告接口，全体人群
     */
    public static String addAnnounce(Announce announce) {
        MessageAnnounce messageAnnounce = new MessageAnnounce();
        messageAnnounce.setType(announce.getType().getName());
        messageAnnounce.setContent(announce.getContent());
        messageAnnounce.setUrl(announce.getUrl());
        String result = messageUtil.messageService.addMessageAnnounce(messageAnnounce);
        return result;
    }

    /**
     * 新增公告接口，指定id集合
     */
    public static String addAnnounceList(Announce announce, List<Long> ids) {
        MessageAnnounce messageAnnounce = new MessageAnnounce();
        messageAnnounce.setType(announce.getType().getName());
        messageAnnounce.setContent(announce.getContent());
        messageAnnounce.setUrl(announce.getUrl());
        String result = messageUtil.messageService.addAnnounceByIds(messageAnnounce, ids);
        return result;
    }

    /**
     * 新增提醒接口，一对一，一次发一个
     */
    public static String addRemind(Remind remind) {
        MessageRemind messageRemind = new MessageRemind();
        messageRemind.setSenderId(remind.getSenderId());
        messageRemind.setSenderName(remind.getSenderName());
        messageRemind.setSenderAction(remind.getSenderAction().getName());
        messageRemind.setObject(remind.getContent());
        messageRemind.setObjectType(remind.getContentType());
        messageRemind.setObjectUrl(remind.getUrl());
        messageRemind.setRecipientId(remind.getRecipientId());
        String result = messageUtil.messageService.addRemind(messageRemind);
        return result;
    }

    /**
     * 新增提醒接口，一对一，一次发多个
     */
    public static String addRemindList(List<Remind> remindList) {
        int communityId = SecurityUtils.getCommunityId();
        Date insertDate = new Date();
        List<MessageRemind> list = new ArrayList<>(remindList.size());
        for(Remind remind : remindList) {
            MessageRemind messageRemind = new MessageRemind();
            messageRemind.setCommunityId(communityId);
            messageRemind.setSenderId(remind.getSenderId());
            messageRemind.setSenderName(remind.getSenderName());
            messageRemind.setSenderAction(remind.getSenderAction().getName());
            messageRemind.setObject(remind.getContent());
            messageRemind.setObjectType(remind.getContentType());
            messageRemind.setObjectUrl(remind.getUrl());
            messageRemind.setRecipientId(remind.getRecipientId());
            messageRemind.setStatus(0);
            messageRemind.setCreateTime(insertDate);
            list.add(messageRemind);
        }
        String result = messageUtil.messageService.addRemindList(list);
        return result;
    }
}
